require 'nokogiri'

def baseline(article, rules)
  sections = nil
  correct = 0
  incorrect = 0

  Dir.mktmpdir do |output_dir|
    cmd="java -jar lib/article-isi/ArticleISI.jar extract --input #{File.dirname(article.xml.path)} --output #{output_dir} --isuffix=\"-blocks.xml\" --osuffix=\"-ns.xml\" --rules=\"#{rules}\" -a"
    #puts cmd
    system(cmd)
    output_file = File.join(output_dir, File.basename(article.xml.path)).gsub("-blocks.xml", "-ns.xml")
    sections = Nokogiri::XML::Document.parse(File.open(output_file))
  end
  f = File.open("/Users/mshirley/tmp.out", "w")
  f.puts sections
  f.close
  sections.xpath('//Block').each do |node|
    tbid = node['id']
    rules_class = node['c']

    true_class = article.tblocks.find_by_tbid(tbid).classification
    if (true_class == nil) or (true_class == "")
      true_class = article.tblocks.find_by_tbid(tbid).ml_class
    end

    #puts "article id: #{article.id}"
    #puts "tbid: #{tbid}"
    #puts "rules_class: #{rules_class}"
    #puts "tb id: #{article.tblocks.find_by_tbid(tbid).id}"
    #puts "true class: #{true_class}"
    #puts
    if true_class == rules_class
      correct += 1
    else
      incorrect += 1
    end
  end
  [correct, incorrect]
end
